function getId(obj){
	return document.getElementById(obj);
}
function addEvent(element,type,fn,capture){
	if(document.attachEvent){
		element.attachEvent("on"+type,fn);
	}
	else{
		element.addEventListener(type,fn,capture);
	}
}
function removeEvent(element,type,fn,capture){
	if(document.detachEvent){
		element.detachEvent("on"+type,fn);
	}
	else{
		element.removeEventListener(type,fn,capture);
	}
}

var menu = {
	trig : getId("menu_bar"),
	panel : getId("menu_wrap"),
	bar_bd : getId("menu_bar_bd"),
	scrollJudge : function(){
		var that = this,
			head_Height = window.parent.document.getElementsByName("topFrame")[0].contentWindow.document.body.clientHeight,
			body_Height = window.parent.document.body.clientHeight,
			con_Height = document.body.clientHeight,
			max_dis = con_Height - body_Height + head_Height;
		if(con_Height >= (body_Height - head_Height)){
			that.trig.style.display = "block";
			//that.trig.style.top = 0;
			//that.panel.style.top = 0;
			var Lframe_height = body_Height - head_Height;
			var bd_h = Lframe_height - max_dis - 14;
			that.bar_bd.style.height = bd_h >50 ? bd_h + "px" : 50 + "px" ;
		}
		else{
			that.trig.style.display = "none";
			that.trig.style.top = 0;
			that.panel.style.top = 0;
		}
	},
	scrollInit:function(){
		menu.scrollJudge();
		var that = this;
		that.trig.onmousedown = function(ev){
			var ev = ev || window.event,
				o_tY = ev.clientY,
				o_trigposY = parseInt(that.trig.style.top),	
				o_panelposY = parseInt(that.panel.style.top);
			addEvent(document,"mousemove",move,false);
			addEvent(document,"mouseup",stop,false);
			function move(ev){
				var ev = ev || window.event,
					head_Height = window.parent.document.getElementsByName("topFrame")[0].contentWindow.document.body.clientHeight,
					body_Height = window.parent.document.body.clientHeight,
					con_Height = document.body.clientHeight,
					max_dis = con_Height - body_Height + head_Height,
					can_dis =  body_Height - head_Height - parseInt(that.bar_bd.style.height) - 14,
					trig_dis = ev.clientY - o_tY + o_trigposY,
					panel_dis;
				if(can_dis >= max_dis){
					panel_dis = ev.clientY - o_tY + o_trigposY;
				}else{
					panel_dis = parseInt((max_dis / can_dis)*(ev.clientY - o_tY)) + (-1*o_panelposY);
				}
				if(trig_dis >=0 && trig_dis < can_dis){
					that.trig.style.top = trig_dis + "px";
					that.panel.style.top = (-1 * panel_dis) + "px";
				}else if(trig_dis<0){
					that.trig.style.top =  "0px";
					that.panel.style.top = "0px";
				}else if(trig_dis >= can_dis){
					that.trig.style.top =  can_dis + "px";
					that.panel.style.top = -max_dis + "px";
				}
			}
			function stop(){
				removeEvent(document,"mousemove",move,false);
				removeEvent(document,"mouseup",stop,false);
			}
			return false;
		}
		addEvent(document,"DOMMouseScroll",wheelMove,false);
		addEvent(document,"mousewheel",wheelMove);
		function wheelMove(ev){
			var ev = ev || window.event,
				o_trigposY = parseInt(that.trig.style.top),
				o_panelposY = parseInt(that.panel.style.top),
				head_Height = window.parent.document.getElementsByName("topFrame")[0].contentWindow.document.body.clientHeight,
				body_Height = window.parent.document.body.clientHeight,
				con_Height = document.body.clientHeight,
				max_dis = con_Height - body_Height + head_Height,
				can_dis =  body_Height - head_Height - parseInt(that.bar_bd.style.height) - 14,
				trig_dis,
				panel_dis,
				mouse_every;
			if(con_Height < (body_Height - head_Height)){
				return;
			}
			if(ev.detail){
				if(ev.detail > 0){
					mouse_every = 12;
				}else{
					mouse_every = -12;
				}
			}else if(ev.wheelDelta){
				if(ev.wheelDelta < 0){
					mouse_every = 12;
				}else{
					mouse_every = -12;
				}
			}
			if(can_dis >= max_dis){
				trig_dis = mouse_every + o_trigposY;
				panel_dis = mouse_every + o_trigposY;
			}else{
				trig_dis = mouse_every + o_trigposY;
				panel_dis = parseInt((max_dis / can_dis) * mouse_every) + (-1 * o_panelposY);
			}
			//console.log("can_dis:" + can_dis + "max_dis:" + max_dis);
			if(trig_dis >= 0 && trig_dis <= can_dis){
				that.trig.style.top = trig_dis + "px";
				that.panel.style.top = (-1 * panel_dis) + "px";
			}else if(trig_dis<0){
				that.trig.style.top =  "0px";
				that.panel.style.top = "0px";
			}else if(trig_dis > can_dis){
				that.trig.style.top = can_dis + "px";
				that.panel.style.top = -max_dis + "px";
			}
		}
	},
	tagInit:function(){
		var that = this,
			item = getId("menu_list").getElementsByTagName("div"),
			len = item.length;
		for(var i = 0;i < len;i++){
			if(item[i].className == "menu"){
				var trig = item[i].getElementsByTagName("h3")[0];
				trig.onclick = function(i){
					var panel = 	item[i].getElementsByTagName("ul");
					return function(){
						if(panel[0].style.display == "" || panel[0].style.display == "block"){
							panel[0].style.display = "none";
							this.className = "close_up";
						}
						else{
							panel[0].style.display = "block";
							this.className = "";
						}
						menu.scrollJudge();
					}
				}(i);
			}
		}
	}
}
window.onresize =resize;
//setInterval("resize()",100);
function resize(){
	menu.scrollJudge();
	menu.scrollInit();
}
menu.scrollInit();
menu.tagInit();